﻿using CLDemo.Data;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;

namespace CLDemo.Modelview
{
    /// <summary>
    /// 最顶层的视图模型。
    /// </summary>
    public class LaunchViewModel : VisibleViewModel
    {
        public LegacyUserViewModel LegacyUserViewModel { get; private set; }

        public ObservableCollection<LegacyPlayerInfo> LegacyPlayerInfos { get; set; }

        public LaunchGameViewModel GameDirViewModel { get; private set; }

        #region command

        public class RemoveCommand : ICommand
        {

            public event EventHandler? CanExecuteChanged;

            private LaunchViewModel _launchViewModel;

            public RemoveCommand(LaunchViewModel launchViewModel)
            {
                _launchViewModel = launchViewModel;
            }

            public bool CanExecute(object? parameter)
            {
                return true;
            }

            public void Execute(object? parameter)
            {
                if (parameter is Selector == false) return; 

                var selector = parameter as Selector;
                var targetObject = selector.SelectedItem;
                
                
                if (targetObject == null || targetObject is LegacyPlayerInfo == false)
                {
                    MessageBox.Show("请选择一个项目进行删除");
                }
                else
                {
                    _launchViewModel.LegacyPlayerInfos.Remove(targetObject as LegacyPlayerInfo);
                }

            }

        }

        public class SelectCommand : ICommand
        {
            private LaunchViewModel _launchViewModel;

            public SelectCommand(LaunchViewModel launchViewModel)
            {
                _launchViewModel = launchViewModel;
            }

            public event EventHandler? CanExecuteChanged;

            public bool CanExecute(object? parameter)
            {
                return true;
            }

            public void Execute(object? parameter)
            {
                MessageBox.Show("Icommand");
            }
        }

        #endregion

        public ICommand RemoveCmd { get; private set; }
        public ICommand SelectCmd { get; private set; }
        public LaunchViewModel() : base(null)
        {
            LegacyUserViewModel = new LegacyUserViewModel(this);
            GameDirViewModel = new LaunchGameViewModel(this);
            RemoveCmd = new RemoveCommand(this);
            SelectCmd = new SelectCommand(this);
            ReloadSubViewModel(new VisibleViewModel[] { LegacyUserViewModel, GameDirViewModel });

            LegacyPlayerInfos = Data.GeneralData.LegacyPlayerInfos;
        }


    }
}
